from blog.models import *
from django.contrib import admin


#class PostAdmin(admin.ModelAdmin):
#    fields = ['author', 'title', 'text', 'date']
#    list_display = ['author', 'title', 'date']
#    date_hierarchy = 'date'
#    search_fields = ['author', 'title']
#    list_filter = ['author', 'date']
#
#admin.site.register(Post, PostAdmin)

admin.site.register(Status)
admin.site.register(Event)

class ShotInline(admin.TabularInline):
    model = Shot
    fk_name = 'sequence'
    extra = 4
    fields = ['name', 'status', 'order']

class SequenceAdmin(admin.ModelAdmin):
    fieldsets = [
        (None,      {'fields': ['name', 'status', 'order']}),
        ('Tracking',{
                    'fields': ['priority', 'depend', 'start_date', 'due_date'],
                    'classes': ['collapse']
                    }
        ),
    ]
    list_display = ['name', 'status', 'order']
    date_hierarchy = 'create_date'
    search_fields = ['name', 'status', 'order']
    list_filter = ['project', 'name', 'create_date']
    inlines = [ShotInline]

admin.site.register(Sequence, SequenceAdmin)

class SequenceInline(admin.TabularInline):
    model = Sequence
    fk_name = 'project'
    extra = 0
    fields = ['name', 'status', 'order']

    inlines = [ShotInline]

    can_delete = False
    
class LibInline(admin.TabularInline):
    model = Lib
    fk_name = 'project'
    extra = 0
    fields = ['name', 'status', 'desc']

    inlines = [ShotInline]

    can_delete = False

class CharAdmin(admin.ModelAdmin):
    fieldsets = [
        (None,      {'fields': ['lib', 'family', 'name', 'status', 'hasHair', 'hasCloth']}),
        ('Tracking',{
                    'fields': ['priority', 'depend', 'start_date', 'due_date'],
                    'classes': ['collapse']
                    }
        ),
    ]
    list_display = ['family', 'name', 'status',]
    date_hierarchy = 'create_date'
    search_fields = ['familly', 'name', 'status']
    list_filter = ['project', 'name', 'hasHair', 'hasCloth']
    #inlines = [LibInline]
admin.site.register(Char, CharAdmin)

class ProjectAdmin(admin.ModelAdmin):
    # Overrides
    def THIS_DONT_WORK_formfield_for_foreignkey(self, db_field, request, **kwargs):
        '''
        Available status fields depends on the current status.
        THIS DOES NOT WORK since we can't get the current object's status
        '''
        #NB: the current user is request.user
        print self.queryset.__doc__
        if db_field.name == "statusX":
            currentStatus = '???'
            qs = Status.objects.filter(after=currentStatus)
            # TODO: Is this needed or would be automatique:
            qs = qs.filter(pk=currentStatus.pk) # Adds it to the set... not obvious!
            kwargs["queryset"] = qs
        return super(ProjectAdmin, self).formfield_for_foreignkey(db_field, request, **kwargs)
    
    def get_readonly_fields(self, request, obj=None):
        '''
        Set the name as read only unless we are creating
        a new one.
        '''
        readonly_fields = list(super(ProjectAdmin, self).get_readonly_fields(request, obj))
        #obj is None is case of creation page:
        if obj is not None:
            readonly_fields.append('name')
        return readonly_fields
    
    # Actions
    def dummy_action(self, request, queryset):
        #rows_updated = queryset.update(status='p')
        #if rows_updated == 1:
        #    message_bit = "1 story was"
        #else:
        #    message_bit = "%s stories were" % rows_updated
        self.message_user(request, "This action is just a dummy (ran on %i objects)." % len(queryset))
    actions = ['dummy_action']
    
    # Project List page
    list_display = ['name', 'label', 'desc', 'status']
    list_editable = ['label', 'status']
    ordering = ['create_date']
    list_filter = ['create_date', 'priority',]
    date_hierarchy = 'create_date'
    search_fields = ['name', 'status__name', 'desc']
    
    #filter_vertical = ['name']

    # Project Edit/Create page
    fieldsets = [
        (None,      {'fields': ['name', 'label', 'status', 'desc']}),
        ('Tracking',{
                    'fields': [('priority', 'depend'), ('start_date', 'due_date')],
                    'classes': ['collapse']
                    }
        ),
    ]
    inlines = [SequenceInline,LibInline]

    # Affects Both
    # That would be neet:
    #formfield_overrides = {
    #    models.ForeignKey: {'widget': ForeignKey_WithKoolStatus},
    #}

admin.site.register(Project, ProjectAdmin)




